﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectEuler
{
    class Problem3
    {
        public decimal Solve()
        {
            // largest prime factor of the number 600851475143
            var n = 600851475143;

            var largestPrimeFactor = 0;
            if(n % 2 == 0)
            {
                largestPrimeFactor = 2;
                do
                {
                    n = n / 2;
                }
                while ((n % 2 == 0));                   
            }
            if(n % 3 == 0)
            {
                largestPrimeFactor = 3;
                do
                {
                    n = n / 3;
                }
                while ((n % 3 == 0)); 
            }

            var multOfSix = 6;
            while(multOfSix - 1 < n)
            {
                if(n % (multOfSix - 1) == 0)
                {
    	            largestPrimeFactor = multOfSix - 1;
    	            while(n % largestPrimeFactor == 0)
                        n = n / largestPrimeFactor;
                }

                if(n % (multOfSix + 1) == 0)
                {
    	            largestPrimeFactor = multOfSix - 1;
                    while (n % largestPrimeFactor == 0)
                        n = n / largestPrimeFactor;
                }
                multOfSix += 6;
                Console.WriteLine("MultOf6: " + multOfSix);
            }

            return largestPrimeFactor;
        }
    }
}
